﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.IO;
using QLHD.Models;
using System.Globalization;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Xml;
using QLHD.Utilities;
using System.Web.Script.Serialization;

namespace QLHD
{
    public partial class ContractsManagement : System.Web.UI.Page
    {
        public string userpermistion = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            userpermistion = "0";

            if (Request.Params["_ct"] != null && Request.Params["_ct"].ToLower() == "notify")
            {
                lttitle.Text = "DANH SÁCH HỢP ĐỒNG SẮP HẾT HẠN";
            }

            if (Request.Params["v"] != null && Request.Params["v"] == "1")
            {
                Session["viewonly"] = true;
                userpermistion = "1";
            }
            else
            {
                Session["viewonly"] = false;
                List<PermissionsUser> gps = new AuthenticationHelper().GetUserPermissions();
                int usp = 0;
                if (gps.Where(q => q.ObjectID == 1).Count() > 0)
                    usp = gps.Where(q => q.ObjectID == 1).Max(q => q.PermissionValue);
                userpermistion = usp.ToString();
            }

            HttpCookie cookie = Request.Cookies.Get("dsdoitac");
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                QLHD2013Entities db = new QLHD2013Entities();
                var lstdoitac = db.DoiTacs.Select(q => q.TenDoiTac);
                string doitac = "\"" + string.Join("\",\"", lstdoitac) + "\"";
                cookie = new HttpCookie("dsdoitac", doitac);
                cookie.Expires = DateTime.Now.AddMinutes(2);
                Response.Cookies.Add(cookie);
            }


            string jsphongban = "[]";




            List<PhongBan> viewphongbanids = new AuthenticationHelper().GetListPhongban();
            //if (viewphongbanids[0].ID > 0)
            //    viewphongbanids.Insert(0, new PhongBan() { ID = 0, Ten = "-" });
            var pbs = ((from item in viewphongbanids
                        select new
                        {
                            ID = item.ID,
                            Ten = item.Ten
                        })).ToList();

            JavaScriptSerializer jsserialize = new JavaScriptSerializer();
            jsphongban = jsserialize.Serialize(pbs);

            ClientScript.RegisterStartupScript(this.GetType(), "doitacsuggest", "partners=[" + cookie.Value + "]; departmentss=" + jsphongban + ";", true);
        }

        public void UpdateSearchContent(HopDong topct)
        {
            string path = Server.MapPath("~/Agreement.xml");

            List<Type> types = new List<Type>();
            types.Add(typeof(XNKL));
            types.Add(typeof(KhoanChiPhi));

            using (XmlWriter writer = XmlWriter.Create(path, new XmlWriterSettings()))
            {
                DataContractSerializer serializer = new DataContractSerializer(topct.GetType(), types);
                serializer.WriteObject(writer, topct);
            }
        }
    }
}